package com.boc.langxiang.ui.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.ConcernsActivityContract;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.presenter.me.ConcernsActivityPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.activity.index.ArticleDetailActivity;
import com.boc.langxiang.ui.adapter.me.ConcernsRVAdapter;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的关注
 */
public class ConcernsActivity extends BaseToolBarActivity implements ConcernsActivityContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.recylerview_me_concerns)
    RecyclerView recylerviewMeConcerns;
    @BindView(R.id.swipeLayout_me_concerns)
    SwipeRefreshLayout swipeLayoutMeConcerns;

    private int mPage;
    private ConcernsRVAdapter mAdapter;
    private ConcernsActivityPresenter mPresenter;
    private boolean mIsOnRefresh;
    private boolean mIsLoadmore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_concern);
        ButterKnife.bind(this);
        setToolBarTitle("我的关注");

        mPresenter = new ConcernsActivityPresenter(this);
        swipeLayoutMeConcerns.setOnRefreshListener(this);
        swipeLayoutMeConcerns.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        recylerviewMeConcerns.setHasFixedSize(true);
        recylerviewMeConcerns.setLayoutManager(new LinearLayoutManager(mContext));

        initAdapter();
        mPage = 1;
        mPresenter.concernList(String.valueOf(mPage));
        swipeLayoutMeConcerns.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mIsOnRefresh) {
                    return true;
                }
                return false;
            }
        });
    }

    private void initAdapter() {
        mAdapter = new ConcernsRVAdapter(null, this);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) recylerviewMeConcerns.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        recylerviewMeConcerns.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(ConcernsActivity.this, ArticleDetailActivity.class);
                ArticleEntity item = mAdapter.getItem(position);
                intent.putExtra("data", item);
                intent.putExtra("isGuanZhu", true);
                ConcernsActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    protected boolean isSetTypeFace() {
        return true;
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mIsLoadmore = false;
        mAdapter.notifyDataChangedAfterLoadMore(false);
        mIsOnRefresh = false;
        swipeLayoutMeConcerns.setRefreshing(false);
        T.showShort(mContext, msg);
    }

    @Override
    public void onRefresh() {
        if (mIsOnRefresh) {
            return;
        }
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mIsOnRefresh = true;
        mPage = 1;
        mPresenter.concernList(String.valueOf(mPage));
    }

    @Override
    public void onLoadMoreRequested() {
        swipeLayoutMeConcerns.post(new Runnable() {
            @Override
            public void run() {
                if (mIsLoadmore) {
                    return;
                }
                mIsLoadmore = true;
                mPage++;
                mPresenter.concernList(String.valueOf(mPage));
            }
        });
    }


    @Override
    public void setBackgroundAlpht(float v) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = v;
        getWindow().setAttributes(attributes);
    }

    @Override
    public void showPopupWindow(View view, PopupWindow cancelPopupWindow) {
        cancelPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onNotConcern(PopupWindow cancelPopupWindow, String id, int layoutPosition) {
        mPresenter.delConcernlist(id, layoutPosition);
        cancelPopupWindow.dismiss();
    }

    @Override
    public void onCancel(PopupWindow cancelPopupWindow) {
        cancelPopupWindow.dismiss();
    }

    @Override
    public void getConcernList(List<ArticleEntity> list) {
        if (isFinishing()) {
            return;
        }
        if (mIsLoadmore) {
            mIsLoadmore = false;
            if (list == null) {
                mAdapter.notifyDataChangedAfterLoadMore(false);
                loadDone();
                return;
            }

            if (list.size() < Constant.PAGE_SIZE) {
                mAdapter.notifyDataChangedAfterLoadMore(list, false);
                loadDone();
            } else {
                mAdapter.notifyDataChangedAfterLoadMore(list, true);
            }

        } else {
            mIsOnRefresh = false;
            swipeLayoutMeConcerns.setRefreshing(false);
            if (list == null) {
                mAdapter.setNewData(new ArrayList<ArticleEntity>());
                return;
            }
            mAdapter.setNewData(list);
        }
    }

    @Override
    public void freshRequest(int layoutPosition) {
        mAdapter.getData().remove(layoutPosition);
        mAdapter.notifyDataSetChanged();
        Toast.makeText(App.getInstance(), "取消关注成功", Toast.LENGTH_SHORT).show();

    }

    private void loadDone() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) recylerviewMeConcerns.getParent(), false);
        mAdapter.addFooterView(view);
    }

}
